<?php 
namespace app\admin\model;
use think\Model;
use think\Db;
use think\Session;

class Admin extends Model
{
	/**
	 * 管理员密码修改
	 *
	 * @param  [array] $user  [$_POST过来的值]
	 * @param  [array] $admin [当前管理员数据]
	 * @return [int]       	  [用于控制层判断]
	 */
	public function editadmin($user,$admin){
		$oldpass = md5($user['mpass']);
		$data['id'] = $user['id'];
		$data['username'] = $user['username'];
		$data['password'] = md5($user['newpass']);
		if($oldpass == $admin['password']){
			$res = Db::name('admin')->update($data);
			if($res){
				return 1;	//修改密码成功
			}else{
				return 2;	//修改密码失败
			}
		}else{
			return 3;	//密码错误
		}
	}


	/**
	 * 用户登录验证
	 * 
	 * @param  [array] $login [$_POST过来的值]
	 * @return [int]          [用于控制层判断]
	 */
	public function login($login){
		$username = $login['username'];
		$password = md5($login['password']);
		//检查是否存在该用户
		$res_username = Db::name('admin')
							->where('username',$username)
							->find();
		//验证登录信息
		$res = Db::name('admin')
					->where('username',$username)
					->where('password',$password)
					->find();

		if(!$res_username){
			return 1;	//不存在该管理员
		}
		if($res){
			session('username', $username);
			return 3;	//登录成功
		}else{
			return 2;	//密码错误
		}
	}

	/**
	 * 无限分类的遍历
	 * @param  [array]  $data      所有数据
	 * @param  integer $parentsid parentsid为0时是一级分类
	 * @param  integer $level     记录几级分类
	 * @return [array]            已经遍历好的数组
	 */
	public function category($data,$parentsid=0,$level=0){
		static $array = array();
		foreach ($data as $key => $value) {
			if($value['parentsid'] == $parentsid){
				$value['level'] = $level;
				$array[] = $value;
				$this->category($data,$value['cateid'],$level+1);
			}
		}
		return $array;
	}

	

	public function piccateid($data,$id){
		static $piccateid = array();
		foreach ($data as $key => $value) {
			if($value['parentsid'] == $id){
				$piccateid[] = $value['cateid'];
				$this->piccateid($data,$value['cateid']);
			}
		}
		return $piccateid;
	}



	public function search($page,$each,$dbtable,$post=0)
	{
		if(request()->isPost()){
			$arr_position[] = $post['ishome'];
			$arr_position[] = $post['isvouch'];
			$arr_position[] = $post['istop'];
			//$arr_position = array_filter($arr_position);
			foreach ($arr_position as $k => $v) {
				$position[] = '%'.$v.'%';
			}

			$keywords = $post['keywords'];
			Session::set('position',$position,'News/search');
			Session::set('keywords',$keywords,'News/search');
		}

		$position = Session::get('position','News/search');
		$keywords = Session::get('keywords','News/search');
		
		if ($position) {	//查询数据
			if ($keywords) {
				$data = db($dbtable)
						->where('position','like',$position)
						->where('name','like','%'.$keywords.'%')
						->order('sort,id')
						->select();	
			}else{
				$data = db($dbtable)
						->where('position','like',$position)
						->order('sort,id')
						->select();	
			}
		}else{
			if ($keywords) {
				$data = db($dbtable)
						->where('name','like','%'.$keywords.'%')
						->order('sort,id')
						->select();	
			}else{
				$data = db($dbtable)->select();	
			}
		}

		//$data = db($dbtable)->select();	
		$sum = count($data);	//总条数
		$pagetotal = ceil($sum/$each);	//总页数,尾页
		//限制手动在URL上输入不存在的页面
		if($sum !== 0){
			if($page>$pagetotal || $page<1){
				return -1;
				//$this->error('没有此页面');
			}
		}
		//$list = db($dbtable)->limit(($page-1)*$each,$each)->select();
		//当前页的数据,从第($page-1)*$each条数据起取$each条数据
		if ($position) {	//查询数据
			if ($keywords) {
				$list = db($dbtable)
						->where('position','like',$position)
						->where('name','like','%'.$keywords.'%')
						->limit(($page-1)*$each,$each)
						->order('sort,id')
						->select();	
			}else{
				$list = db($dbtable)
						->where('position','like',$position)
						->limit(($page-1)*$each,$each)
						->order('sort,id')
						->select();	
			}
		}else{
			if ($keywords) {
				$list = db($dbtable)
						->where('name','like','%'.$keywords.'%')
						->limit(($page-1)*$each,$each)
						->order('sort,id')
						->select();	
			}else{
				$list = db($dbtable)->limit(($page-1)*$each,$each)->order('sort')->select();	
			}
		}

		$res['list'] = $list;
		$res['pagetotal'] = $pagetotal;
		$res['page'] = $page;
		return $res;
	}

	public function page($page,$each,$dbtable)
	{
		$data = db($dbtable)->select();	//查询数据
		$sum = count($data);	//总条数
		$pagetotal = ceil($sum/$each);	//总页数,尾页
		//限制手动在URL上输入不存在的页面
		if($sum !== 0){
			if($page>$pagetotal || $page<1){
			return -1;
			//$this->error('没有此页面');
			}
		}
		
		//当前页的数据,从第($page-1)*$each条数据起取$each条数据
		$list = db($dbtable)
				->limit(($page-1)*$each,$each)
				->order('sort,id')
				->select();	

		$res['list'] = $list;
		$res['pagetotal'] = $pagetotal;
		$res['page'] = $page;
		return $res;
	}

	public function picpage($page,$each,$dbtable)
	{
		$data = db($dbtable)->select();	//查询数据
		$sum = count($data);	//总条数
		$pagetotal = ceil($sum/$each);	//总页数,尾页
		//限制手动在URL上输入不存在的页面
		if($sum !== 0){
			if($page>$pagetotal || $page<1){
				return -1;
				//$this->error('没有此页面');
			}
		}
		//当前页的数据,从第($page-1)*$each条数据起取$each条数据
		$table = 'tp_'.$dbtable;
		$list = Db::table($table)
				->alias('pic')
				->join('category cate','pic.cateid = cate.cateid')
				->limit(($page-1)*$each,$each)
				->order('picsort,id')
				->select();	

		$res['list'] = $list;+
		$res['pagetotal'] = $pagetotal;
		$res['page'] = $page;
		return $res;
	}

}